package com.ruicar.afs.jobadmin.controller;

import com.ruicar.afs.jobadmin.core.model.AfsJobInfo;
import com.ruicar.afs.jobadmin.core.model.AfsJobLogGlue;
import com.ruicar.afs.jobadmin.mapper.AfsJobInfoMapper;
import com.ruicar.afs.jobadmin.core.util.I18nUtil;
import com.ruicar.afs.jobadmin.mapper.AfsJobLogGlueMapper;
import com.ruicar.afs.cloud.common.job.core.biz.model.ReturnT;
import com.ruicar.afs.cloud.common.job.core.glue.GlueTypeEnum;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * The type JobCodeController
 * <p>Description: </p>
 *
 * @author Fzero
 * @version 1.0
 * @date Created on 2020.06.13 14:17:13
 * @since 1.0
 */
@Controller
@RequestMapping("/jobcode")
public class JobCodeController {

    /**
     * Afs job info mapper
     * <p>Description: </p>
     */
    @Resource
    private AfsJobInfoMapper afsJobInfoMapper;
    /**
     * Afs job log glue mapper
     * <p>Description: </p>
     */
    @Resource
    private AfsJobLogGlueMapper afsJobLogGlueMapper;

    /**
     * Index string
     *
     * @param request request
     * @param model   model
     * @param jobId   job id
     * @return the string
     */
    @RequestMapping
    public String index(HttpServletRequest request, Model model, int jobId) {
        AfsJobInfo jobInfo = afsJobInfoMapper.loadById(jobId);
        List<AfsJobLogGlue> jobLogGlues = afsJobLogGlueMapper.findByJobId(jobId);

        if (jobInfo == null) {
            throw new RuntimeException(I18nUtil.getString("jobinfo_glue_jobid_unvalid"));
        }
        if (GlueTypeEnum.BEAN == GlueTypeEnum.match(jobInfo.getGlueType())) {
            throw new RuntimeException(I18nUtil.getString("jobinfo_glue_gluetype_unvalid"));
        }

        // Glue类型-字典
        model.addAttribute("GlueTypeEnum", GlueTypeEnum.values());

        model.addAttribute("jobInfo", jobInfo);
        model.addAttribute("jobLogGlues", jobLogGlues);
        return "jobcode/jobcode.index";
    }

    /**
     * Save return t
     *
     * @param model      model
     * @param id         id
     * @param glueSource glue source
     * @param glueRemark glue remark
     * @return the return t
     */
    @RequestMapping("/save")
    @ResponseBody
    public ReturnT<String> save(Model model, int id, String glueSource, String glueRemark) {
        // valid
        if (glueRemark == null) {
            return new ReturnT<String>(500, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_glue_remark")));
        }
        if (glueRemark.length() < 4 || glueRemark.length() > 100) {
            return new ReturnT<String>(500, I18nUtil.getString("jobinfo_glue_remark_limit"));
        }
        AfsJobInfo exists_jobInfo = afsJobInfoMapper.loadById(id);
        if (exists_jobInfo == null) {
            return new ReturnT<String>(500, I18nUtil.getString("jobinfo_glue_jobid_unvalid"));
        }

        // update new code
        exists_jobInfo.setGlueSource(glueSource);
        exists_jobInfo.setGlueRemark(glueRemark);
        exists_jobInfo.setGlueUpdatetime(new Date());

        exists_jobInfo.setUpdateTime(new Date());
        afsJobInfoMapper.update(exists_jobInfo);

        // log old code
        AfsJobLogGlue afsJobLogGlue = new AfsJobLogGlue();
        afsJobLogGlue.setJobId(exists_jobInfo.getId());
        afsJobLogGlue.setGlueType(exists_jobInfo.getGlueType());
        afsJobLogGlue.setGlueSource(glueSource);
        afsJobLogGlue.setGlueRemark(glueRemark);

        afsJobLogGlue.setAddTime(new Date());
        afsJobLogGlue.setUpdateTime(new Date());
        afsJobLogGlueMapper.save(afsJobLogGlue);

        // remove code backup more than 30
        afsJobLogGlueMapper.removeOld(exists_jobInfo.getId(), 30);

        return ReturnT.SUCCESS;
    }

}
